package com.luo.ai.assistant.file.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luo.ai.assistant.file.model.PageQuery;

/**
 * 基础Mapper
 *
 * @author luohq
 * @date 2025-01-07 11:00
 */
public interface MyBaseMapper<T> extends BaseMapper<T> {

    /**
     * 转换PageQuery为Mybatis-Plus分页参数
     *
     * @param pageQuery 分页查询参数
     * @return Mybatis-Plus分页参数
     */
    default Page toPage(PageQuery pageQuery) {
        Page page = Page.of(pageQuery.getPageIndex(), pageQuery.getPageSize());
        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(pageQuery.getOrderBy())) {
            OrderItem orderItem = PageQuery.ASC.equals(pageQuery.getOrderDirection())
                    ? OrderItem.asc(pageQuery.getOrderBy())
                    : OrderItem.desc(pageQuery.getOrderBy());
            page.addOrder(orderItem);
        }
        return page;
    }
}
